/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ownPackage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

/**
 *
 * @author aisyah
 */
public class FindReplace {
    /**
     * Fetch the entire contents of a text file, and return it in a String.
     * This style of implementation does not throw Exceptions to the caller.
     *
     * @param aFile is a file which already exists and can be read.
     */
    static public String getContents(File aFile) {
        //...checks on aFile are elided
        StringBuilder contents = new StringBuilder();

        try {
            //use buffering, reading one line at a time
            //FileReader always assumes default encoding is OK!
            BufferedReader input = new BufferedReader(new FileReader(aFile));
            try {
                String line = null; //not declared within while loop
        /*
                 * readLine is a bit quirky :
                 * it returns the content of a line MINUS the newline.
                 * it returns null only for the END of the stream.
                 * it returns an empty String if two newlines appear in a row.
                 */
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return contents.toString();
    }

    static public void replace(File aFile,String allcontents, String oldContents, String newContents){
        PrintStream p;

        FileOutputStream outf = null;
        try {
            outf = new FileOutputStream(aFile);
            p = new PrintStream(outf);
            String hasil = allcontents.replaceAll(oldContents, newContents);
            p.println(hasil);
            p.close();
//			System.out.println("File "+username1+".txt"+" created");
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

    }
}
